package com.cuixk.microservice.hystrix.controller;

import com.cuixk.microservice.hystrix.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getUserInfoHystrix", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), //请求次数
    })
    public User getUserInfo(@PathVariable Integer id) {
        if (id == 1) {
            throw new NullPointerException();
        }

        User user = new User();
        user.setId(id);
        user.setName("provider");
        user.setAge(25);
        return user;
    }

    public User getUserInfoHystrix(Integer id) {
        User user = new User();
        user.setName("error");
        return user;
    }
}
